/*
注意！！！
mainframe.js只能在top页面中引用。
*/
//在当前页面打开对话框

var mainframe = {
  openedDialogs : {},
  defaultDialogId : "dk_top_dialog_default",
  defaultShowToggle : false,
  hideDialogs : {},
  minDialogs : {},


	/**
	 *
	 * @param options
	 * {
	 * parentFrameId 执行本函数的元素所在IFrame的id，在childframe中通过getSelfFrameId()获取
	 * dialogId 新弹出窗口的id
	 * title 弹出窗口的标题
	 * url
	 * height
	 * width
	 * iframeName 新弹出窗口iframe的name
	 * isHidden
	 * isModal true|false
	 * position 子窗口的位置，可以为null { left: left, top: top }
	 * buttons
	 * }
	 */
	openDialog : function(options){
		if(!options)return;
		var that = this;
		var dialogId = options.id || options.dialogId;

		var dialog = dkDialog.get(dialogId);
		if (dialog) {
			dialog.close();//如果窗口已经存在，则先关闭窗口
		}


		var dialog = dkDialog.openDialog(options);

		this.openedDialogs[dialogId]=dialog;

		var parentFrameId = options.parentFrameId;
		var dialogFrameId = "layui-layer-iframe"+ dialog.layerId;
		$("#"+dialogFrameId).attr("parentFrameId", parentFrameId);//add by wanghua 重要，不能删除。用来实现弹出窗口和父窗口两个iframe之间的函数调用


		dialog.bind('onClosed',function(){
			debugger;
			delete that.openedDialogs[dialogId];
			if(that.minDialogs[dialogId] && top.mapFrame){
				var minDialogList = top.mapFrame.document.getElementById("minDialogList");
				$("#"+dialogId,$(minDialogList)).remove();
				delete that.minDialogs[dialogId];
			}
		});

		//最小化
		dialog.bind('onMinized',function (){
			that.minDialog(dialogId);
	  	});


		if(dialogId=='helpLocation'){
			$("#helpLocation").css("z-index", "9300").attr("resetzindex",false);
		}

		return dialog;
	},
	 /**
	 *
	 * @param parentFrameId 执行本函数的元素所在IFrame的id，在childframe中通过getSelfFrameId()获取
	 * @param dialogId 新弹出窗口的id
	 * @param title 弹出窗口的标题
	 * @param url
	 * @param height
	 * @param width
	 * @param iframeName 新弹出窗口iframe的name
	 * @param isHidden 点击关闭按钮时，是彻底关闭还是仅隐藏
	 * @param isModal true|false
	 * @param position 子窗口的位置，可以为null { left: left, top: top }
	 * @param buttons
	 * @returns
	 */
	openChildDialog : function(parentFrameId, dialogId, title, url, width, height, iframeName,isHidden,isModal,position,buttons) {

	dialogId = dialogId || this.defaultDialogId;
		iframeName=iframeName||dialogId;
	if(url.indexOf("?")==-1){
			url=url+"?dialogId="+dialogId;
		}else{
			url=url+"&dialogId="+dialogId;
		}


	var options = {};
	options.parentFrameId = parentFrameId;
	options.id = dialogId;
	options.title = title;
	options.url = url;
	options.width = width;
	options.height = height;
	options.modal = isModal;
	options.isHidden = isHidden;//此参数无效
	if(position){
		if(position.left || position.left === 0) options.left = position.left;
		if(position.top || position.top === 0) options.top = position.top;
	}
	options.btn = buttons;

	this.openDialog(options);
	},

	//打开主框架界面内的对话框
	openMainFrameDialog : function(title, url, dialogId, width, height, iframeName,isHidden,buttons){
		return this.openChildDialog(null, dialogId,title, url, width, height, iframeName,isHidden,buttons);
	},

	//刷新对话框内容
	refreshDialogContent : function(dialogId,url){
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(dialog&&dialog.get("visible")){
			dialog.setUrl(url);
		}
	},
	closeDialog : function(dialogId){
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(typeof dialogId=="object"){
			dialog=dialogId;
		}
		if(dialog){
			var isHidden=dialog.get("isHidden");
			if(isHidden){
				dialog.hide();
				if(this.minDialogs[dialogId]&&top.mapFrame){
					var minDialogList=top.mapFrame.document.getElementById("minDialogList");
					$("#"+dialogId,$(minDialogList)).remove();
					delete this.minDialogs[dialogId];
				}
			}else{
				dialog.close();
			}
		}
	},

	hideDialog : function (dialogId){
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(typeof(dialogId)=="object"){
			dialog=dialogId;
		}
		if(dialog&&dialog.get("visible")){
			dialog.hide();
		}
	},


	//隐藏所有打开的对话框
	hideAllDialog : function (){
		for(var dialogId in this.openedDialogs){
			var dialog = this.openedDialogs[dialogId];
			if(dialog.get("visible")){
				dialog.hide();
				this.hideDialogs[dialogId]=dialog;
			}
		}
	},

	//打开使用hideAllDialog隐藏的对话框
	showAllDialog : function (){
		for(var dialogId in this.hideDialogs){
			this.showDialog(dialogId);
		}
		this.hideDialogs={};
	},

	showDialog : function (dialogId){
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(dialog){
			dialog.show();
			dialog.active();
			if(this.minDialogs[dialogId]&&top.mapFrame){
				var minDialogList=top.mapFrame.document.getElementById("minDialogList");
				$("#"+dialogId,$(minDialogList)).remove();
				delete this.minDialogs[dialogId];
			}
		}
	},
	//最小化对话框窗口到主窗口下的minDialogList层内；
	minDialog : function(dialogId){
		if(!top.mapFrame)return;
		var minDialogList=top.mapFrame.document.getElementById("minDialogList");
		if(!minDialogList){
			return;
		}
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(dialog){
			dialog.hide();
			var dialogTitle=dialog.get("title");
			if(dialogTitle.length>6){
				dialogTitle = dialogTitle.substr(0,5)+"...";
			}
			var minDialogdiv='<div class="minDialog" id="'+dialogId+'">\
				<div style="width:85px;float:left">\
				<span class="minDialogTitle" onclick=javascript:top.window.showDialog("'+dialogId+'") title="'+dialog.get("title")+'">'+dialogTitle+'</span></div>\
				<div style="width:26px;float:right;margin-top:4px;">\
				<span onclick=javascript:top.window.closeDialog("'+dialogId+'") title="关闭对话框" class="closeBtn">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div></div>';
			$(minDialogList).append(minDialogdiv);
			this.minDialogs[dialogId]=dialogId;
		}
	},

	//最小化所有对话框
	minAllDialog : function (){
		for(var dialogId in this.openedDialogs){
			var dialog = this.openedDialogs[dialogId];
			if(dialog.get("visible")){
				this.minDialog(dialogId);
			}
		}
	},

	//最大化所有最小化的对话框
	maxAllDialog : function (){
		for(var dialogId in this.minDialogs){
			this.showDialog(dialogId);
		}
		this.minDialogs={};
	},

	setDialogTitle : function(dialogId,title){
		var dialog = this.openedDialogs.get(dialogId);
		if(dialog&&dialog.get("visible")){
			dialog.set("title",title);
		}
	},

	//遮罩id=dialogId以外的所有元素
	maskWindow : function(dialogId){
		var dialog=dkDialog.get(dialogId);
		if(dialog&&dialog.get("visible")){
			dialog.active();
			//$(".l-window-mask").css("z-index",($("#"+dialogId).css("z-index")-1));
			dialog.mask();
		}
	},

	getDialog : function(dialogId){
		return dkDialog.get(dialogId)||top.window.dkDialog.get(dialogId);
	},

	/**
	 * 读取iframe页面内的变量,或函数
	 * @param varName 变量名或函数名
	 * @param iframeName 变量所在页面的名字
	 */
	getValueOfChildFrame : function(iframeName, varName){
		iframeName=iframeName||"cacheDataIframe";
		var iframeDoc=top.window.frames[iframeName];
		var varName="iframeDoc."+varName;
		if(iframeDoc){
			return eval(varName);
		}
		return undefined;
	},
	/**
	 * 修改iframe页面内的变量
	 * @param varName 变量名或函数名
	 * @param iframeName 变量所在页面的名字
	 */
	setValueOfChildFrame : function(iframeName, varName,value){
		iframeName=iframeName||"cacheDataIframe";
		var iframeDoc=top.window.frames[iframeName];
		if(iframeDoc){
			iframeDoc[varName]=value;
			return true;
		}
		return false;
	},
	/**
	 * 调用指定dialog中的方法
	 * @param {} dialogId
	 * @param {} funcName
	 * @param {} args
	 * @return {}
	 */
	invokeFunctionOfChild : function(dialogId, funcName, args){

		var iframeDoc=top.window.frames[dialogId];

		var funcName2="iframeDoc."+funcName;
		if(iframeDoc){
			var func = eval(funcName2);
			//alert(args);
			return func.apply();
		}
		return undefined;
	},

	/**
	 * 读取iframe页面内的变量,或函数
	 * @param varName 变量名或函数名
	 * @param iframeName 变量所在页面的名字
	 */
	getValueOfMainFrame : function(varName){
		return top.window[varName];
	}
};

if(!top.mainframe){
	top.mainframe = mainframe;
}
